package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xgkj.diyiketang.activity.data.FansListAdapter;

import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.AddFriendTask;
import com.xgkj.diyiketang.task.SearchAnchorTask;
import com.xgkj.diyiketang.task.SearchUserTask;
import com.xgkj.diyiketang.util.EmptyDataUtil;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.util.ToastUtil;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;
import com.xgkj.diyiketang.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XCKJ_Paladowow on 2016/3/21.
 */
public class SearchChiBanListActivity extends BaseActivity implements PullToRefreshListView.OnRefreshListener,PullToRefreshListView.OnLoadDataListener,FansListAdapter.FansListAdapterListener {
    private Context mContext;
    private PullToRefreshListView mListView;
    private FansListAdapter mAdapter;
    private int currentPage = 1;
    private int size = 10;
    private int searchTaskId = -1;
    private String keywords;
    private TextView back;
    private LinearLayout mEmptyView;

    private LinearLayout mEmptyContentView;
    private int addFriendTaskId = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_chiban_list);
        mContext = this;
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        if (intent != null) {
            keywords = intent.getStringExtra("keyword");
        }
        mEmptyView = (LinearLayout) findViewById(R.id.content_error_empty);
        mEmptyView.setVisibility(View.GONE);
        mEmptyContentView = (LinearLayout) findViewById(R.id.empty_content_view);
        mEmptyContentView.setVisibility(View.GONE);
        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mListView = (PullToRefreshListView)findViewById(R.id.search_chiban_pull_list);
        mListView.init(mContext,true);
        mAdapter = new FansListAdapter(mContext);
        mAdapter.setAdapterListener(this);
        mListView.setAdapter(mAdapter,true);
//        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                if(mListView.getHeaderViewsCount()>0){
//                    position--;
//                }
//                if (position<0)return;
//                EatShowUserInfo info = (EatShowUserInfo) mAdapter.getItem(position);
//                startActivity(AnchorActivity.getIntent(mContext, info.getUserId()));
//            }
//        });
        mListView.setOnRefreshListener(this);
        mListView.setOnLoadDataListener(this);
        mListView.doRefresh();


    }


    private void getPaiHaiBang() {
        if(searchTaskId != -1){
            return;
        }
        if(currentPage == 0){
            mListView.setNoNextPage();
            return;
        }

        SearchUserTask task = new SearchUserTask(keywords,size, currentPage);
        task.addListener(anchorListListener);
        searchTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    private TaskEventListener anchorListListener = new ShowToastTaskEventListener(this){
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if(taskId == searchTaskId){
                searchTaskId = -1;
                if(currentPage == 1){
                    mAdapter.clearData();
                    mListView.onRefreshComplete();
                    mListView.enableLoadMore();
                }else{
                    mListView.onLoadDataComplete(true);
                }
                List<EatShowUserInfo> fansList = new ArrayList<EatShowUserInfo>();
                if(data != null){
                    fansList = (List<EatShowUserInfo>) data;
                }

                if(fansList != null && fansList.size()>0){
                    mAdapter.updateData(fansList);
                }

                currentPage = result;

                if(mAdapter != null && mAdapter.getCount() == 0){
                    EmptyDataUtil.updateWenziEmptyContentView(mEmptyView, mEmptyContentView, mListView);
                }else{
                    EmptyDataUtil.updateNoEmptyContentView(mEmptyView, mEmptyContentView, mListView);
                }

            }else if (taskId==addFriendTaskId){
                ToastUtil.showMessage(mContext,R.string.apply_to_friend_alreay_send);
            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if(taskId == searchTaskId){
                searchTaskId = -1;
                if(currentPage == 1){
                    mListView.onRefreshComplete();
                }else{
                    mListView.onLoadDataComplete(false);
                }
            }
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }

    };


    public void onHiddenChanged(boolean hidden) {
        onHiddenChanged(hidden);
        if(!hidden){
            //mListView.invalidate();
            if(mAdapter != null && mAdapter.getCount() == 0){
                mListView.doRefresh();
            }
        }
    }

    @Override
    public void onPullRefresh() {

    }

    @Override
    public void onLoadMore() {
        getPaiHaiBang();
    }

    @Override
    public void onRefresh() {
        currentPage = 1;
        getPaiHaiBang();

    }

    @Override
    public void onClick(String userId) {
        addFriend(userId);
    }

    private void addFriend(String userId){
        AddFriendTask task = new AddFriendTask(userId);
        task.addListener(anchorListListener);
        addFriendTaskId = getEatShowApp().getTaskManager().addTask(task);
    }
}
